package com.woniu.deal.order.controller;

import com.woniu.common.dto.DealInsertOrderDTO;
import com.woniu.common.dto.DealOrderDTO;
import com.woniu.common.entity.DealOrder;
import com.woniu.common.entity.Result;
import com.woniu.deal.order.service.DealOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/order")
public class DealOrderController {

    @Autowired
    DealOrderService dealOrderService;

    //我的订单页面
    @GetMapping("/")
    public Result getAllOrdersByUserId(HttpServletRequest request){
        Integer userId = Integer.valueOf(request.getHeader("userId"));
        System.out.println(userId);
        List<DealOrder> dealOrders = dealOrderService.getOneAllOrders(userId);
        System.out.println(dealOrders);
        return Result.ok(dealOrders);
    }

    //分页条件查询
    @PostMapping("{pageIndex}/{pageSize}")
    public Result getAllByConditionPage(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody DealOrderDTO dealOrderDTO
    ) {
        return Result.ok(dealOrderService.getListByConditionPage(pageIndex, pageSize, dealOrderDTO));
    }

    //下单页面
    @PostMapping("/")
    public Result<String> addNewOrder(HttpServletRequest request,HttpServletResponse response,@RequestBody DealInsertOrderDTO dealInsertOrderDTO){
        String orderNum = dealOrderService.addOrder(request,dealInsertOrderDTO);
        response.setHeader("orderNum",orderNum);
        response.setHeader("Access-Control-Expose-Headers","*");
        return Result.ok("添加订单成功,订单编号为："+orderNum);
    }

    @PutMapping("/")
    public Result modify(@RequestBody DealOrder dealOrder){
        dealOrderService.modify(dealOrder);
        return Result.ok("修改成功");
    }

    @GetMapping("/{id}")
    public Result<DealOrder> getDealOrderById(@PathVariable("id") int id){
       DealOrder dealOrder = dealOrderService.getOneDealOrderById(id);
       return Result.ok(dealOrder);
    }

}
